<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('reviewRiskList.riskSetting.table.type')">
          <el-select v-model="searchForm.type" :placeholder="$t('reviewRiskList.riskSetting.prompt.type')">
            <el-option
              v-for="item in reviewType"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reviewRiskList.riskSetting.table.status')">
          <el-select v-model="searchForm.status" :placeholder="$t('reviewRiskList.riskSetting.prompt.status')">
            <el-option
              v-for="item in $t('reviewRiskList.riskSetting.status1')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reviewRiskList.riskSetting.table.name')">
          <el-input v-model="searchForm.paramName" :placeholder="$t('reviewRiskList.riskSetting.prompt.name')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportData">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        class="Top"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          :label="tableText['number']"
          min-width="50"
        />
        <el-table-column
          prop="paramName"
          :label="tableText['name']"
          min-width="150"
        />
        <el-table-column
          prop="descriptor"
          :label="tableText['remarks']"
          min-width="150"
        />
        <el-table-column
          prop="type"
          :label="tableText['type']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ setType(scope.row['type']) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="params"
          :label="tableText['value']"
          min-width="80"
        >
          <template slot-scope="scope">
            {{ scope.row['params'] ? JSON.parse(JSON.stringify(scope.row['params'])).join(',') : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="exemptionDays"
          :label="tableText['status']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ setState(scope.row['status'], 'riskSetting.state') }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editInfo(scope.row)">{{ $t('common.operate.edit') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <risk-review-dialog
      :params-obj="paramsObj"
      :keys-obj="keysObj"
      :user-social-identity-code="userSocialIdentityCode"
      :hidden-state="hiddenState"
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
  </el-row>
</template>

<script>
import { getRiskReview, getUserSocialIdentityCode, exportRiskReview } from '@/api/risk'
import riskReviewDialog from '@/components/risk/businessConfig/risk-review-dialog'
export default {
  name: 'RiskReview',
  components: {
    riskReviewDialog
  },
  data() {
    return {
      loading: true,
      editDialog: false,
      reviewType: [],
      hiddenState: false,
      tableText: {},
      totalNumber: 0,
      tableData: [],
      paramsObj: null,
      keysObj: null,
      userSocialIdentityCode: null,
      searchForm: this.params(),
      chooseItem: {
        id: null,
        descriptor: null,
        paramName: null,
        params: null,
        status: null,
        type: null
      }
    }
  },
  mounted() {
    const obj = [{
      desc: this.$t('common.all'),
      value: ''
    }]
    const type = JSON.parse(JSON.stringify(this.$t('reviewRiskList.riskSetting.reviewType'))).splice(2, 4)
    this.reviewType = obj.concat(type)
    this.tableText = this.$t('reviewRiskList.riskSetting.table')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        type: '',
        status: '',
        paramName: null
      }
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    exportData() {
      exportRiskReview(this.searchForm)
    },
    getData() {
      this.loading = true
      getRiskReview(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    setType(data) {
      const name = this.reviewType.filter(item => {
        return item.value === data
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    setState(state, data) {
      const map = this.$t(`reviewRiskList.${data}`)
      const name = map.filter(item => {
        return item.value === state
      })
      return (name && name[0] && name[0].desc) ? name[0].desc : ''
    },
    editInfo(item) {
      if (item.id === 23) {
        this.hiddenState = true
        getUserSocialIdentityCode()
          .then(res => {
            if (res.success) {
              this.userSocialIdentityCode = res.data
              this.keysObj = Object.keys(this.userSocialIdentityCode)
              const params = (item.params[0]).toString().split('&')
              const obj = {
                id: item.id,
                descriptor: item.descriptor,
                paramName: item.paramName,
                params: params,
                status: item.status,
                type: item.type
              }
              this.choose(obj)
              this.editDialog = true
            } else {
              this.$message.error(res.message)
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error(err.message)
          })
      } else if (item.id === 42) {
        this.hiddenState = true
        this.userSocialIdentityCode = this.$t('reviewRiskList.riskSetting.education')
        this.keysObj = Object.keys(this.$t('reviewRiskList.riskSetting.education'))
        const params = (item.params[0]).toString().split('&')
        const obj = {
          id: item.id,
          descriptor: item.descriptor,
          paramName: item.paramName,
          params: params,
          status: item.status,
          type: item.type
        }
        this.choose(obj)
        this.editDialog = true
      } else if (item.id === 43) {
        this.hiddenState = true
        this.userSocialIdentityCode = this.$t('reviewRiskList.riskSetting.province')
        this.keysObj = Object.keys(this.$t('reviewRiskList.riskSetting.province'))
        const params = (item.params[0]).toString().split('&')
        const obj = {
          id: item.id,
          descriptor: item.descriptor,
          paramName: item.paramName,
          params: params,
          status: item.status,
          type: item.type
        }
        this.choose(obj)
        this.editDialog = true
      } else {
        this.hiddenState = false
        this.paramsObj = JSON.parse(JSON.stringify(item.params))
        this.choose(item)
        this.editDialog = true
      }
    },
    changeDialog(param) {
      this.editDialog = param
      setTimeout(() => {
        this.choose({})
      }, 10)
    },
    editSuccess() {
      this.getData()
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.descriptor = param.descriptor
      this.chooseItem.paramName = param.paramName
      this.chooseItem.params = param.params
      this.chooseItem.status = param.status
      this.chooseItem.type = param.type
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
